package gk.example.activity;

import gk.example.BaseActivity;
import gk.example.view.ClipImageView;

import java.io.ByteArrayOutputStream;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.gkdate.R;

public class ClipActivity extends BaseActivity implements OnClickListener {
	private String mPathName;
	private Button mBtnClip;
	private TextView mTvTitle;
	private ClipImageView mClipImageView;
	private ImageButton mBtnBack;

	@Override
	protected void initView() {
		setContentView(R.layout.activity_clip);
		mClipImageView = (ClipImageView) findViewById(R.id.clip_image_view);
		mBtnClip = (Button) findViewById(R.id.btn_clip);
		mTvTitle = (TextView) findViewById(R.id.tv_top_title);
		mBtnBack = (ImageButton) findViewById(R.id.btn_back);
	}

	@Override
	protected void initEvent() {
		mBtnClip.setVisibility(View.VISIBLE);
		mBtnBack.setOnClickListener(this);
		mBtnClip.setOnClickListener(this);
		mTvTitle.setText("设置头像");
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		mPathName = bundle.getString("photoPath");
		mClipImageView.setImageBitmap(BitmapFactory.decodeFile(mPathName));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_clip:
			Bitmap bitmap = mClipImageView.clip();
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
			byte[] bitmapByte = baos.toByteArray();
			Intent intent = new Intent(this, PreviewActivity.class);
			intent.putExtra("bitmap", bitmapByte);
			intent.putExtra("path", mPathName);
			startActivity(intent);
			finish();
			break;
		case R.id.btn_back:
			startActivity(new Intent(this, PersonalInfoActivity.class));
			finish();
			break;
		default:

			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			startActivity(new Intent(this, PersonalInfoActivity.class));
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}

}
